package com.egao.oa.service.impl;

import com.egao.common.core.damon.constant.Birthday;
import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.DateUtil;
import com.egao.common.core.damon.utils.PhoneUtils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.common.system.service.EmailService;
import com.egao.oa.biz.PhoneBookBiz;
import com.egao.oa.entity.PhoneBook;
import com.egao.oa.service.PhoneBookService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//import com.egao.oa.mapper.PhoneBookMapper;

/**
 * 通讯录服务实现类
 * @author : Doman
 * @date : 2020-08-19 10:53:21
 * @version ：1.0
 */
@Service
public class PhoneBookServiceImpl /*extends ServiceImpl<PhoneBookMapper, PhoneBook> */implements PhoneBookService {

	@Autowired
	private PhoneBookBiz phoneBookBiz;
    @Autowired
    private EmailService emailService;

    /**
    @Override
    public PageResult<PhoneBook> listPage(PageParam<PhoneBook> page) {
        List<PhoneBook> records = baseMapper.listPage(page);
        return new PageResult<>(records, page.getTotal());
    }

    @Override
    public List<PhoneBook> listAll(Map<String, Object> page) {
        return baseMapper.listAll(page);
    } */


    @Scheduled(cron="0 0 9 * * ?")
//    @Scheduled(cron="0/15 * * * * ?")
    public void birthday()  throws Exception{
        User loginUser = new User();
        loginUser.setUserId(UserConstant.ADMIN_ID);
        List<PhoneBook> list = phoneBookBiz.list(new PhoneBook(),loginUser);
        String bd = DateUtil.getCurrentDate("MM-dd");
        for(PhoneBook pb: list) {
            if (DateUtil.formatDate(DateUtil.parseDate(pb.getBirthday(), "yyyy-MM-dd"),"MM-dd").equals(bd)) {
                String phone = "";
                try {
                    phone = pb.getPhone().substring(0,11);
                } catch (Exception e) {

                }
                if(phone.equals("13186850898") || phone.equals("13028978666")) {
                    PhoneUtils.sendMsg("13186850898,13028978666", "632416", new String[]{pb.getName(),Birthday.BIRTHDAY_STRS[(int)(Math.random()*11)]},1);
                } else {
                    PhoneUtils.sendMsg("13186850898,13028978666,"+phone, "632416", new String[]{pb.getName(),Birthday.BIRTHDAY_STRS[(int)(Math.random()*11)]},1);
                }
                emailService.sendTextEmail("宁波甩手掌柜网络科技有限公司祝"+ pb.getName() +"生日快乐!", Birthday.BIRTHDAY_STRS[(int)(Math.random()*11)], new String[]{"416252350@qq.com","2542387704@qq.com"});
            }
            String mon = DateUtil.getCurrentDate("MM");
            if(DateUtil.getCurrentDate("dd").equals("01")){
                if (DateUtil.formatDate(DateUtil.parseDate(pb.getBirthday(), "yyyy-MM-dd"),"MM").equals(mon)) {
                    PhoneUtils.sendMsg("15869512287", "632416", new String[]{pb.getName(),Birthday.BIRTHDAY_STRS[(int)(Math.random()*11)]},1);
                }
            }
        }
    }

    public static void main(String[] args) {
        String s = "1997-07-20";
        System.out.println(DateUtil.formatDate(DateUtil.parseDate(s, "yyyy-MM-dd"),"MM-dd"));
        System.out.println(s.substring(5,7));
        System.out.println(DateUtil.getCurrentDate("dd"));
    }

    @Override
    public JsonResult list(PageResult2<T> page, PhoneBook phoneBook, User loginUser)  throws Exception{
        return phoneBookBiz.list(page,phoneBook,loginUser);
    }

    @Override
    public List<PhoneBook> list(PhoneBook phoneBook, User loginUser) {
        return phoneBookBiz.list(phoneBook,loginUser);
    }

    @Override
    public JsonResult getById(PhoneBook phoneBook) {
        return JsonResult.ok().setData(phoneBookBiz.getById(phoneBook));
    }

    @Transactional
    @Override
    public JsonResult save(PhoneBook phoneBook, User loginUser) {
        if (phoneBookBiz.save(phoneBook, loginUser)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @Transactional
    @Override
    public JsonResult update(PhoneBook phoneBook, User loginUser) {
        if (phoneBookBiz.update(phoneBook, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult remove(PhoneBook phoneBook, User loginUser) {
        if (phoneBookBiz.remove(phoneBook, loginUser)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

}
